package com.flying.invest.assets.spider;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.flying.common.http.JsonHttpUtils;
import com.flying.framework.annotation.Param;
import com.flying.framework.model.ModelFactory;
import com.flying.framework.service.AbstractService;

public class EastMoneyPriceSpider extends AbstractService implements IPriceSpider {

	public SpiderPrice fetch(
			@Param(value="code", required=false,	desc="URL") String code) throws Exception {
		String url = this.getUrl(code);
		System.out.println(">>>>>>:"+url);
		JSONObject jo= JsonHttpUtils.get(url);
		JSONArray arr = jo.getJSONObject("data").getJSONArray("details");
		String s = (String)arr.get(arr.size() - 1);
		String[] tmps = s.split(",");
		Double price = Double.parseDouble(tmps[1]);
		return ModelFactory.create(SpiderPrice.class, SpiderPrice.PRICE, price, SpiderPrice.URL, url);
	}

	public String getUrl(String code) {
		String market = this.getServiceConfig().getConfig("market");
		return "http://push2.eastmoney.com/api/qt/stock/details/get?fields1=f1,f2,f3,f4&fields2=f51,f52,f53,f54,f55&pos=-11&secid="+market+"."+code+"&cb=";
	}
}
